package hd.util.bitmap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Shader;

import hd.util.ScreenHelper;


/**
 * 穿件软件里所使用的一些位图
 * <p/>
 * Created by haidy on 14-9-15.
 */
public class BitmapFactory {

    /**
     * 绘制seekBar的拖动图片
     *
     * @param size 尺寸
     * @return 位图
     */
    public static Bitmap drawSeekBarThumb(int size) {
        Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(new LinearGradient(size / 2, 0, size / 2, size,
                new int[]{Color.rgb(0xd2, 0xd2, 0xd2),
                        Color.rgb(0xaf, 0xaf, 0xaf)},
                null,
                Shader.TileMode.REPEAT));
        paint.setShadowLayer(4, 0, 4, Color.parseColor("#40000000"));
        canvas.drawCircle(size / 2, size / 2, size / 3, paint);
        return bitmap;
    }


    /**
     * 绘制单色的bitmap（一般用作背景）
     *
     * @param width  宽度
     * @param height 高度
     * @param color  颜色
     * @return 位图
     */
    public static Bitmap drawColorBitmap(int width, int height, int color) {
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(color);
        return bitmap;
    }

    /**
     * 绘制刷新按钮
     *
     * @param context 上下文
     * @param size    尺寸
     * @param color   颜色
     * @return bitmap
     */
    public static Bitmap drawRefreshImage(Context context, int size, int color) {
        Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.translate(size / 2f, size / 2f);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(ScreenHelper.dp2px(context, 1));

        float radius = size / 3.5f;
        canvas.drawCircle(0, 0, radius, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(size / 2f, size / 2f);
        path.lineTo(size / 2f, -size / 2f);

        paint.setStyle(Paint.Style.FILL);
        canvas.drawPath(path, paint);

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(ScreenHelper.dp2px(context, 1));

        int line = ScreenHelper.dp2px(context, 6);
        float a = (float) ((radius - line) / Math.sqrt(2));
        float b = (float) ((radius + line) / Math.sqrt(2));
//        System.out.println(radius + " " + a + " " + b);
        path = new Path();
        path.moveTo(a, -a);
        path.lineTo(b, -b);
        path.lineTo(b, -a);
        canvas.drawPath(path, paint);

        return bitmap;
    }

    /**
     * 货值软件默认的背景
     *
     * @param width  宽度
     * @param height 高度
     * @param radius 圆角半径
     * @param color  颜色
     * @return 位图
     */
    public static Bitmap drawRoundSwitchBackground(int width, int height, int radius, int color) {
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRoundRect(new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()), radius, radius, paint);
        paint.setColor(Color.rgb(0xc3, 0xc3, 0xc3));
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRoundRect(new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()), radius, radius, paint);

        return bitmap;
    }

}